//
//  JkmWelcomeViewController.swift
//  SinaWb
//
//  Created by Jiang on 16/5/15.
//  Copyright © 2016年 Jiang. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

private weak var weakself : AnyObject?
class JkmWelcomeViewController: UIViewController {
    
    
    let userinfo : UserModel = ViewModel.sharViewModel.UserInfo!

    lazy var welcomeLable : UILabel = {
        let lable = UILabel()
        lable.text = "欢迎回来"
        lable.font = UIFont.systemFontOfSize(18)
        lable.textColor = UIColor.lightGrayColor()
        lable.sizeToFit()
        weakself!.view.addSubview(lable)
        return lable
    }()
    lazy var nameLable : UILabel = {
//        weak var weakself = self
        let lable = UILabel()
        lable.text = weakself!.userinfo.name!
        lable.font = UIFont.systemFontOfSize(14)
        lable.textColor = UIColor.lightGrayColor()
        lable.sizeToFit()
        weakself!.view.addSubview(lable)
        return lable
    }()
    lazy var circleview :UIImageView = {
        let imageview  = UIImageView(image: UIImage(named: "avatar_default_big"))
        imageview.layer.cornerRadius = 45
        imageview.layer.masksToBounds = true
        imageview.alpha = 0
        weakself!.view.addSubview(imageview)
        return imageview
    }()
    
    override func loadView() {
        weakself = self
        view  = UIImageView(image: UIImage(named: "ad_background"))
        
        
        circleview.snp_makeConstraints { (make) in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-200)
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
        nameLable.snp_makeConstraints { (make) in
            make.top.equalTo(circleview.snp_bottom)
            make.centerX.equalTo(view.snp_centerX)
        }
        welcomeLable.snp_makeConstraints { (make) in
            make.top.equalTo(nameLable.snp_bottom)
            make.centerX.equalTo(nameLable.snp_centerX)
            
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        

        guard userinfo.avatarURL != nil else{
            return
        }
        circleview.sd_setImageWithURL(userinfo.avatarURL!)

    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        circleview.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(-view.bounds.height + 290)
        }
        welcomeLable.alpha = 0
        nameLable.alpha = 0
        circleview.alpha = 1.0
        UIView.animateWithDuration(2.0, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: {
            self.view.layoutIfNeeded()
            
            }) { (_) in
                UIView.animateWithDuration(1, animations: {self.nameLable.alpha = 1 ; self.welcomeLable.alpha  =  1}, completion: { (_) in
                    
                    NSNotificationCenter.defaultCenter().postNotificationName("JkmAppDelegate", object: nil, userInfo: [" ": " "])
                })
               
        }
    }
    deinit{
        JkLog("欢迎界面 died")
    }
    
}
